#pragma once

#include "../comm/log.hpp"
#include "../comm/sqlManager.hpp""

struct Video {
    int video_id;                // 视频的唯一标识符
    std::string title;           // 视频标题
    std::string description;     // 视频描述
    std::string url;             // 视频链接
    std::string thumbnail_url;   // 缩略图链接
    std::string category;        // 视频类别
    std::string tags;            // 视频标签
};

class VideoManager {
private:
    ns_sql::MySQLManager _dbManager;
    std::mutex _mutex; 

public:
    VideoManager(const std::string &host, const std::string &user, const std::string &password, const std::string &dbName) {
        LOG_DEBUG() << "Initializing VideoManager with database: " << dbName;
        if (!_dbManager.openDatabase(host, user, password, dbName)) {
            LOG_ERROR() << "Failed to open database: " << _dbManager.getErrorMessage();
        }
    }

    ~VideoManager() {
        _dbManager.closeDatabase();
    }

    // 添加视频
    bool addVideo(const std::string &title, const std::string &description, const std::string &url,
                  const std::string &thumbnail_url, const std::string &category, const std::string &tags) {
        std::unique_lock<std::mutex> lock(_mutex);

        // 构建 SQL 查询
        std::string query = "INSERT INTO videos (title, description, url, thumbnail_url, category, tags) VALUES ('" +
                            title + "', '" + description + "', '" + url + "', '" + thumbnail_url + "', '" + 
                            category + "', '" + tags + "')";

        // 执行插入语句
        bool result = _dbManager.executeQuery(query);
        if (!result) {
            LOG_ERROR() << "Failed to add video: " << _dbManager.getErrorMessage();
            return false;
        }

        return true;
    }

    // 删除视频
    bool deleteVideo(const std::string &videoId) {
        std::unique_lock<std::mutex> lock(_mutex);

        // 构造删除查询语句，根据 video_id 删除视频
        std::string query = "DELETE FROM videos WHERE video_id = " + videoId;

        // 执行删除语句
        bool result = _dbManager.executeQuery(query);
        if (!result) {
            LOG_ERROR() << "Failed to delete video: " << _dbManager.getErrorMessage();
            return false;
        }

        LOG_DEBUG() << "Video deleted successfully.";
        return true;
    }

    // 获取所有视频
    bool getAllVideos(std::vector<Video> &videoList) {
        std::unique_lock<std::mutex> lock(_mutex);

        // 构造查询语句
        std::string query = "SELECT video_id, title, description, url, thumbnail_url, category, tags FROM videos";

        // 获取查询结果
        ns_sql::MySQLManager::MySQLResult result = _dbManager.getResult(query);

        // 检查是否有数据
        if (!result.next()) {
            LOG_ERROR() << "No videos found or query failed: " << _dbManager.getErrorMessage();
            return false;
        }

        // 处理结果集并填充到 videoList
        do {
            try {
                Video video;
                video.video_id = result.getInt(0);
                video.title = result.getString(1);
                video.description = result.getString(2);
                video.url = result.getString(3);
                video.thumbnail_url = result.getString(4);
                video.category = result.getString(5);
                video.tags = result.getString(6);

                videoList.push_back(video);
            } catch (const std::exception &e) {
                LOG_ERROR() << "Error processing video data: " << e.what();
                return false;
            }
        } while (result.next());

        return true;
    }
};
